import { Context,Application } from "egg";
import * as FS from 'fs';
import * as PATH from "path";
import * as mime from "mime";
interface MyStaticOptions {
  path: string;
}

export default function (options:MyStaticOptions,app:Application) {
  // console.log(options, app);
  return async function (ctx: Context, next: () => Promise<any>) {
    // console.log(ctx.path.indexOf("/api"));
    if (ctx.path.indexOf("/api")>-1) {
      await next();
      
    }else{
      // console.log(mime.getType(ctx.path));
      ctx.path = ctx.path === "/" ? "/index.html" : ctx.path;
      ctx.type = mime.getType(ctx.path);
      // console.log(ctx.type);
      ctx.body = FS.createReadStream(
        PATH.join(app.baseDir, "app", options.path, ctx.path)
      );
      // ctx.body = "1";
    }
   
  };
}
